package app.server.project.main.service.db.entity.user;

import app.server.project.core.constants.Def;
import app.server.project.core.syndb.SynDBMgr;
import app.server.project.core.util.MathUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Transient;
import lombok.Data;
import org.hibernate.annotations.Comment;

import java.time.LocalDateTime;

@Data
@Entity(name = "user_active_reward")
@TableName("user_active_reward")
@Comment(value = "用户活动奖励表")
public class UserActiveRewardEntity {

    @Id
    @Column(name = "id")
    @Comment("用户主键")
    @TableId("id")
    private long id;

    @TableField("create_time")
    @Comment("创建时间")
    @Column(name = "create_time")
    private LocalDateTime createTime;


    @TableField("invite_num")
    @Comment("邀请人数")
    @Column(name = "invite_num")
    private long inviteNum;


    @TableField("share_flag")
    @Comment("多重奖励发放")
    @Column(name = "share_flag")
    private int shareFlag;

    @TableField("btc_val")
    @Comment("比特币余额")
    @Column(name = "btc_val")
    private long btcVal;


    @TableField("ton_val")
    @Comment("ton币余额")
    @Column(name = "ton_val")
    private long tonVal;

    @Transient
    @TableField(exist = false)
    private boolean synDb = false;

    public static UserActiveRewardEntity of(long userId) {
        UserActiveRewardEntity userEnergyEntity = new UserActiveRewardEntity();
        userEnergyEntity.setId(userId);
        userEnergyEntity.setSynDb(true);
        userEnergyEntity.setCreateTime(LocalDateTime.now());
        return userEnergyEntity;
    }


    public void setInviteNum(long inviteNum) {
        this.inviteNum = inviteNum;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserActiveRewardEntity::getInviteNum, this);
    }

    public void setTonVal(long tonVal) {
        this.tonVal = tonVal;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserActiveRewardEntity::getTonVal, this);
    }

    public void addTonVal(long tonVal) {
        var ret = MathUtil.canAddNum(this.tonVal, tonVal);
        if (Def.Zero >= ret) return;
        this.setTonVal(this.tonVal + ret);
    }

    public boolean consumeTonVal(long tonVal) {
        if (tonVal > this.tonVal) return false;
        this.setTonVal(this.tonVal - tonVal);
        return true;
    }

    public void setBtcVal(long btcVal) {
        this.btcVal = btcVal;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserActiveRewardEntity::getBtcVal, this);
    }

    public void addBtcVal(long btcVal) {
        var ret = MathUtil.canAddNum(this.btcVal, btcVal);
        if (Def.Zero >= ret) return;
        this.setBtcVal(this.btcVal + ret);
    }

    public void addInviteNum(long num) {
        var ret = MathUtil.canAddNum(this.inviteNum, num);
        if (Def.Zero >= ret) return;
        this.setInviteNum(this.inviteNum + ret);
    }


    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserActiveRewardEntity::getCreateTime, this);
    }

    public void setShareFlag(int shareFlag) {
        this.shareFlag = shareFlag;
        if (!this.synDb) return;
        SynDBMgr.addChangeData(UserActiveRewardEntity::getShareFlag, this);
    }

    public void addShareFlag(int shareFlag) {
        this.shareFlag += shareFlag;
        this.setShareFlag(this.shareFlag);
    }

}
